package com.probox.modules.controller.order;

import cn.hutool.core.util.ObjectUtil;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.order.OrderInfo;
import com.probox.common.entity.service.order.IOrderInfoService;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.order.OrderInvoice;
import com.probox.common.entity.service.order.IOrderInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 开票信息Controller
 *
 * @author hds
 * @date 2021-06-07
 */
@RestController
@RequestMapping("/order/invoice")
public class OrderInvoiceController extends BaseController {
    @Autowired
    private IOrderInvoiceService orderInvoiceService;

    @Autowired
    private IOrderInfoService orderInfoService;


    /**
     * 查询开票信息列表
     */
    @PreAuthorize(hasPermi = "order:invoice:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderInfo orderInfo) {
        if(ObjectUtil.isEmpty(orderInfo.getStoreId())){
            orderInfo.setStoreId(SecurityUtils.getStoreId());
        }
        startPage();
        List<OrderInfo> orderInfos = orderInfoService.selectListByInvoice(orderInfo);
        return getDataTable(orderInfos);
    }

    /**
     * 导出开票信息列表
     */
    @PreAuthorize(hasPermi = "order:invoice:export")
    @Log(title = "开票信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInvoice orderInvoice) throws IOException {
        List<OrderInvoice> list = orderInvoiceService.queryList(orderInvoice);
        ExcelUtil<OrderInvoice> util = new ExcelUtil<OrderInvoice>(OrderInvoice.class);
        util.exportExcel(response, list, "开票信息数据");
    }

    /**
     * 获取开票信息详细信息
     */
    @PreAuthorize(hasPermi = "order:invoice:query")
    @GetMapping(value = "/{recordUid}")
    public AjaxResult getInfo(@PathVariable("recordUid") Long recordUid) {
        return AjaxResult.success(orderInvoiceService.selectOrderInvoiceById(recordUid));
    }

    /**
     * 新增开票信息
     */
    @PreAuthorize(hasPermi = "order:invoice:add")
    @Log(title = "开票信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderInvoice orderInvoice) {
        return toAjax(orderInvoiceService.addInvoice(orderInvoice));
    }


//    /**
//     * 修改开票信息
//     */
//    @PreAuthorize(hasPermi = "order:invoice:edit")
//    @Log(title = "开票信息", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody OrderInvoice orderInvoice) {
//        return toAjax(orderInvoiceService.updateOrderInvoice(orderInvoice));
//    }
//
//    /**
//     * 删除开票信息
//     */
//    @PreAuthorize(hasPermi = "order:invoice:remove")
//    @Log(title = "开票信息", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{recordUids}")
//    public AjaxResult remove(@PathVariable Long[] recordUids) {
//        return toAjax(orderInvoiceService.deleteOrderInvoiceByIds(recordUids));
//    }
}
